{// 引入标签库 }
<tagLib name="html" />
{// 加载头部公共文件 }
<include file="../Public/header" />
<script language="JavaScript">
<!--
	function buildTable(tableId,data) { 
	for (i=0;i<data.length;i++){ 
	  var row =$(tableId).insertRow(-1);
	  if (i==0)
	  {row.className = 'over';
	  }
	  for (j=0;j<data[i].length;j++) { 
		var cell = document.createElement("td");
		cell.innerHTML = data[i][j];
		row.appendChild(cell); 
		//var cell=row.insertCell(-1);
		//cell.innerText=data[i][j];
	   }
	 }
	 }
	function deleteTable(tableId){
		var Table	=	$(tableId);
			while (Table.rows.length > 0) {
				Table.deleteRow(0);
			}
	}

	function handleSql(data){
		document.getElementById('resultNum').innerHTML=	 '';
		deleteTable('table1');
		deleteTable('table2');
		var time = data.splice(0,1);
		var bench = data.splice(0,1);
		if (bench[0])
		{		
			document.getElementById('benchbox').style.display = 'block';
			buildTable('table2',bench[0]);
		}else{
			document.getElementById('benchbox').style.display = 'none';
		}
		if(data.length<=1) {
			document.getElementById('resultNum').innerHTML	=	'[ 返回0条查询记录 耗时:'+time+' ] ';
		}else {
			document.getElementById('resultNum').innerHTML	=	'[ 返回'+(data.length-1)+'条查询记录 耗时:'+time+' ] ';
			buildTable('table1',data);
		}
	}
	function runSql(){
		ThinkAjax.send('__URL__/execute','ajax=1&sql='+document.getElementById('execSql').value,handleSql);
	}

	function executeSql(sql){
		var tableName	=	$F('tableName');	
		if (tableName == '')
		{
		alert('没有选择数据表');return ;
		}
		sql	 =	 sql.replace('$table',tableName);
		if ($F('explain'))
		{
			sql = 'EXPLAIN '+sql;
		}
		document.getElementById('execSql').value	=	sql;
		bench	 =	 $F('bench');
		if (undefined == bench)
		{bench = 0;
		}
		record	 =	 $F('record');
		if (undefined == record)
		{record = 0;
		}
		ThinkAjax.send('__URL__/execute','ajax=1&record='+record+'&bench='+bench+'&sql='+sql,handleSql);
	}
	function showTable(){
			if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;
		}
		executeSql('SHOW COLUMNS FROM $table');
	}
	function clearTable(){
		var tableName	=	$F('tableName');	
		if (tableName == '')
		{
		alert('没有选择数据表');return ;
		}
		if (window.confirm('确实清空表'+tableName+'吗？'))
		{
		executeSql('TRUNCATE TABLE $table');
		}
	}
	function checkTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;
		}
		executeSql('CHECK TABLE $table');
	}
	function searchTable(){
		executeSql('SELECT * FROM $table LIMIT 0,30');
	}
	function optimzeTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;
		}
		executeSql('OPTIMIZE TABLE $table');
	}
	function analyzeTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;
		}
		executeSql('ANALYZE TABLE $table');
	}
	function repairTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;
		}
		executeSql('REPAIR TABLE $table');
	}
	function statusTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;
		}
		executeSql('SHOW TABLE STATUS LIKE  \'$table\'');
	}
	function delTable(){
		var tableName	=	$F('tableName');	
		if (tableName == '')
		{
		alert('没有选择数据表');return ;
		}
		if (window.confirm('确实删除表'+tableName+'吗？'))
		{
		executeSql('DROP TABLE  $table');
		}
	}
	function outputTable(){
		var tableName	=	$F('tableName');
		window.location.href = '__URL__/output/table/'+tableName;
	}
	function importSql(){
		window.location.href = '__URL__/import';
	}
	function refleshDb(db){
		document.getElementById('useDb').innerHTML = db;
		ThinkAjax.send('__URL__/getTables','ajax=1&db='+db,fleshTable);
	}
	function fleshTable(data,status){
		if (status==1)
		{
			document.getElementById('tableName').options.length = 0;
			// 动态增加
			for (j=0;j<data.length;j++) 
			{
			document.getElementById('tableName').options.add( new Option(data[j],data[j]) );
			}
		}

	}
//-->
</script>
<!-- 菜单区域  -->

<!-- 主页面开始 -->
<div id="main" class="main" >
<!-- 主体内容  -->
<div class="content" >
<div class="title">首页 <span style="font-weight:normal;">[当前数据库:<span style="color:blue" id="useDb">{$Think.session.useDb}</span>]</span></div>
<div class="operate" >
<html:imageBtn name="add" value="建库"  click="window.location.href='__URL__/addDb'" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="add" value="建表"  click="add()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="ok" value="导入" click="importSql()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="sort" value="导出"  click="outputTable()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="clear" value="清空"  click="clearTable()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="delete" value="删除"  click="delTable()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="adv" value="高级"  click="window.location.href='__URL__/adv'" style="impBtn hMargin fLeft shadow" />
</div>
<!--  功能操作区域  -->
<div class="cBoth">
<!-- SQL执行器 -->
<form method=post action="">
	<fieldset id="querybox" >
<legend style="color:gray">
<div class="fLeft">在数据库</div>
<select name="database" class="fLeft hMargin" onchange="refleshDb(this.options[this.options.selectedIndex].value)">
<volist name="dbs" id="db">
<neq name="db.Database" value="information_schema">
<option value="{$db.Database}" <eq name="db.Database" value="$useDb">selected</eq>> {$db.Database} </option>
</neq>
</volist>
   </select>
<div class="fLeft">中执行SQL语句（<span style="color:orange">请慎用</span>） <input type="checkbox" name="bench" value="1"> 性能分析 <input type="checkbox" name="explain" value="1"> 查询解释 <input type="checkbox" name="record" value="1"> 记录SQL</div></legend>
<div class="cBoth"></div>
<div id="dbSql" class="cBoth vMargin">
<div class="fLeft"><TEXTAREA id="execSql" NAME="sql" check="Require" warning="输入要执行的SQL语句！" style="width:480px;height:200px;overflow:auto;line-height:155%" >{$Think.get.sql}</TEXTAREA></div>
<div id="dbSql" >
<div class="fLeft hMargin">
<select NAME="tableName" id="tableName" class="medium" size="12" multiple >
<iterate name="tables" id="table" >
<option value="{$table}">{$table}</option>
</iterate>
</select></div>
<div class="fLeft" style="width:65px">
<html:imageBtn value="查询" click="searchTable()" style=" fLeft shadow" />
<html:imageBtn value="结构" click="showTable()" style=" fLeft shadow" />
<html:imageBtn value="优化" click="optimzeTable()" style="  fLeft shadow" />
<html:imageBtn value="检查" click="checkTable()" style="  fLeft shadow" />
<html:imageBtn value="分析" click="analyzeTable()" style="  fLeft shadow" />
<html:imageBtn value="修复" click="repairTable()" style="  fLeft shadow" />
<html:imageBtn value="状态" click="statusTable()" style="  fLeft shadow" />
</div>
</div>
</div>
<br class="cBoth">
<div class="cBoth vMargin" >
<html:imageBtn name="ok" value="执行"  click="runSql()" style="impBtn hMargin fLeft shadow" />
<html:imageBtn name="sort" type="reset" value="重置"  style="impBtn hMargin fLeft shadow" />
</div>
</fieldset>
</form>

<!-- 表操作器 -->
<fieldset id="benchbox" style="display:none">
<legend style="color:gray">数据库性能分析 </legend>
<div id="SqlBench" style="overflow:auto;width:100%;height:100%">
<table id='table2' class="list" style="width:450px"></table>
</div>
</fieldset>
<fieldset id="querybox" >
<legend style="color:gray">数据库查询结果 <span class="blue" id="resultNum"></span> </legend>
<div id="SqlResult" style="overflow:auto;width:100%;height:100%">
<table id='table1' class="list" ></table>
</div>
</fieldset>
</div>
<!-- 列表显示区域结束 -->
</div>
<!-- 主体内容结束 -->
</div>
<!-- 主页面结束 -->
<script language="JavaScript">
<!--
	if ({$Think.get.run|default=0})	runSql();
//-->
</script>
<include file="../Public/footer" />